import java.util.*;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.*;

/** Producer-Consumer in Java, for J2SE 1.5 using concurrent.
 */
public class ProdCons {


	private static final int DEVICES = 50, MAX_TEMP = 50, SLEEP = /*300000*/10000, CREATE=5000;
	private static int[] ids;
	private Random random;
    private Calendar cal;
    private static PhidTest phidTest;

	protected boolean done = false;
	
	Socket socket;
	BufferedOutputStream br;

	/** Inner class representing the Producer side */
	class Producer implements Runnable {

		protected BlockingQueue<String> queue;

		Producer(BlockingQueue<String> theQueue) { this.queue = theQueue; }

		public void run() {
			try {
				while (true) {
					String[] justProduced = createPacket();
					for (String packet : justProduced)
						queue.put(packet);
					System.out.println("Produced 1 object; List size now " + queue.size());
					Thread.sleep(CREATE);
					if (done) {
						return;
					}
				}
			} catch (InterruptedException ex) {
				System.out.println("Producer INTERRUPTED");
			}
		}

		String[] createPacket(){
			String[] packets = new String[DEVICES];
			for (int i=0; i<DEVICES; i++){
				int id = ids[i];
				int temp = random.nextInt(MAX_TEMP);
				packets[i] = "P" + id+ "pT"+temp+"tS"+cal.getTimeInMillis()/1000+"sCcellInfoc";
			}
			return packets;
		}

	}

	/** Inner class representing the Consumer side */
	class Consumer implements Runnable {
		protected BlockingQueue<String> queue;
		private boolean stopped = false;
		private PhidTest phidTest;

		Consumer(BlockingQueue<String> theQueue, PhidTest phidTest) { 
			this.queue = theQueue; 
			this.phidTest = phidTest;
		}

		public void stop() throws InterruptedException{
			stopped = true;
		}
		
		public void run() {
			try {
				while (true) {
					if(!phidTest.isMoving()){
						String obj = queue.take();
						int len = queue.size();
						//System.out.println("List size now " + len);
						//TODO: Communicate on TCP Socket
						try {
							process(obj);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						if (done) {
							return;
						}
					}
				}
			} catch (InterruptedException ex) {
				System.out.println("CONSUMER INTERRUPTED");
			}
		}

		void process(String string) throws IOException {
			// Thread.sleep(xxx) // Simulate time passing
			br.write(string.getBytes());
		}
	}

	ProdCons() throws UnknownHostException, IOException {
		BlockingQueue<String> myQueue = new LinkedBlockingQueue<String>();
		random = new Random();
		ids = new int[DEVICES];
		for (int i =0; i<DEVICES; i++)
			ids[i]=i;
		cal = Calendar.getInstance();
		socket = new Socket("127.0.0.1",1234);
		//Start ACC
		phidTest = new PhidTest();
		Consumer consumer = new Consumer(myQueue, phidTest);

		 br = new BufferedOutputStream(socket.getOutputStream());
		
		new Thread(phidTest).start();
		new Thread(new Producer(myQueue)).start();
		new Thread(consumer).start();
	}

	public static void main(String[] args)
	throws IOException, InterruptedException {

		// Start producers and consumers
		ProdCons pc = new ProdCons();

		// Let the simulation run for, say, 10 seconds
		Thread.sleep(300000*1000); 

		// End of simulation - shut down gracefully
		pc.done = true;
	}
}